package com.wedata.致远接口;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.wedata.HttpUtils;

import java.util.HashMap;
import java.util.Map;

public class 部门管理 {

    public static void main(String[] args) {
        //getDeptByCode(492);
        orgDepartments("1696554104669683737");
        //create();
        //orgDepartment("1696554104669683737");
    }
    /**
     * 获取部门-详情
     * @return
     */
    static JSONObject orgDepartment(String seeyon_id){
        JSONObject dept = HttpUtils.doGet(token.hostUrl + "/rest/orgDepartment/"+seeyon_id+"?token="+token.getToken());
        System.out.println(dept);
        //superior:上级部门Id，一级部门则为所属单位Id
        return dept;

    }

    private static void create(){
        JSONObject header  = new JSONObject();
        header.set("token",token.getToken());

        JSONObject body = new JSONObject();
        body.set("orgAccountId",670869647114347L);
        body.set("code", "497");
        body.set("name", "数字化中台");
        //body.set("description", "测试标识");
        body.set("enabled", true);
        body.set("superior", -1625854415707355017L);
        //body.set("sortId", "497");
        body.set("superiorName", "");
        //String param = "&orgAccountId=670869647114347&code=497&name=测试部门a&enabled=true&superior=1696554104669683737";
        //JSONObject result = HttpUtils.sendPost(token.hostUrl + "/rest/orgDepartment", header, body);
        JSONObject result = HttpUtils.sendPost(token.hostUrl + "/rest/orgDepartment", header, body);
        //String str = HttpUtil.post(token.hostUrl + "/rest/orgDepartment?token="+token.getToken(), body);
        System.out.println(result);
        //{"successMsgs":[{"code":"SUCCESS","ent":{"orgAccountId":670869647114347,"id":2703199873542211784,"name":"数字化中台","i18nNameId":null,"code":"497","createTime":1713756242257,"updateTime":1713756242257,"sortId":553,"isDeleted":false,"enabled":true,"externalType":0,"status":1,"description":"","customerProperties":[],"path":"0000000100140008000200020001","shortName":null,"secondName":null,"isInternal":true,"isGroup":false,"levelScope":-1,"type":"Department","properties":{"zipCode":null,"permissionType":null,"accessLevels":null,"address":null,"isCustomLoginUrl":null,"ipAddress":null,"managementSubunit":null,"telephone":null,"customLoginUrl":null,"isCreateDeptSpace":null,"unitMail":null,"chiefLeader":null,"unitCategory":null,"ldapOu":null,"fax":null},"superior":-1625854415707355017,"superiorName":"财务信息化研发组1","sortIdType":"1","isPublic":false,"createrId":null,"level":0,"entityType":"Department","wholeName":"集团数字化中心/研发部/后端组/财务信息化研发组/数字化中台","depAdmin":null,"depManager":null,"v5External":false,"valid":true,"group":false,"superior0":-1625854415707355017,"customLogin":false,"parentPath":"000000010014000800020002","customLoginUrl":null,"i18nNameWithLocale":"数字化中台","dataI18nCategoryName":"organization.department.name","fullName":"数字化中台","realSortId":553,"preName":"","orgAccountName":"健萌体育产业发展集团有限公司"},"msgInfo":null}],"errorMsgs":[],"errorMsgInfos":[],"success":true}
    }
    static void create2(){
        JSONObject header  = new JSONObject();
        header.set("token",token.getToken());
        JSONArray bodys = new JSONArray();
        //JSONObject body = new JSONObject("{\"orgAccountId\":670869647114347,\"name\":\"财务信息化研发组\",\"code\":\"440\",\"createTime\":1706686422543,\"updateTime\":1712977297620,\"sortId\":546,\"isDeleted\":false,\"enabled\":true,\"externalType\":0,\"status\":1,\"description\":\"\",\"customerProperties\":[],\"path\":\"000000010014000800020002\",\"isInternal\":true,\"isGroup\":false,\"levelScope\":-1,\"type\":\"Department\",\"properties\":{\"isCreateDeptSpace\":0},\"superior\":7865049063101582058,\"superiorName\":\"\",\"sortIdType\":\"1\",\"isPublic\":false,\"level\":0,\"entityType\":\"Department\",\"wholeName\":\"集团数字化中心/研发部/后端组/财务信息化研发组\",\"v5External\":false,\"valid\":true,\"group\":false,\"superior0\":7865049063101582058,\"customLogin\":false,\"parentPath\":\"00000001001400080002\",\"i18nNameWithLocale\":\"财务信息化研发组\",\"dataI18nCategoryName\":\"organization.department.name\",\"fullName\":\"财务信息化研发组\",\"realSortId\":546,\"preName\":\"\",\"orgAccountName\":\"健萌体育产业发展集团有限公司\"}");
        JSONObject body = new JSONObject();
        body.put("orgAccountId",670869647114347L);
        body.set("code", "440");
        body.put("name", "财务信息化研发组");
        body.set("enabled", "true");
        body.put("superior", 7865049063101582058L);
        //body.put("sortId", "497");
        bodys.add(body);
        //String param = "&orgAccountId=670869647114347&code=497&name=测试部门a&enabled=true&superior=1696554104669683737";
        JSONObject result = HttpUtils.sendPost(token.hostUrl + "/rest/orgDepartment/addDepartments", header, bodys);
        System.out.println(result);
        //{"orgAccountId":670869647114347,"id":-1625854415707355017,"name":"财务信息化研发组","code":"440","createTime":1706686422543,"updateTime":1712977297620,"sortId":546,"isDeleted":false,"enabled":true,"externalType":0,"status":1,"description":"","customerProperties":[],"path":"000000010014000800020002","isInternal":true,"isGroup":false,"levelScope":-1,"type":"Department","properties":{"isCreateDeptSpace":0},"superior":7865049063101582058,"superiorName":"","sortIdType":"1","isPublic":false,"level":0,"entityType":"Department","wholeName":"集团数字化中心/研发部/后端组/财务信息化研发组","v5External":false,"valid":true,"group":false,"superior0":7865049063101582058,"customLogin":false,"parentPath":"00000001001400080002","i18nNameWithLocale":"财务信息化研发组","dataI18nCategoryName":"organization.department.name","fullName":"财务信息化研发组","realSortId":546,"preName":"","orgAccountName":"健萌体育产业发展集团有限公司"}
    }
    /**
     * 获取部门-列表
     * @return
     */
    static String orgDepartments(String deptCode){
        if(deptCode==null){
            deptCode = "670869647114347";
        }
        JSONArray depts = HttpUtils.getList(token.hostUrl + "/rest/orgDepartments/670869647114347?token="+token.getToken());

        //System.out.println(depts.size());
        for(int i=0;i<depts.size();i++){
            JSONObject dept = depts.getJSONObject(i);
            if(deptCode.equals(dept.getStr("superior"))){
                System.out.print(dept.getStr("name")+",'");
                System.out.print(dept.getLong("superior")+",'");
                System.out.println(dept.getLong("id"));
            }
        }
        return "";

    }
    /**
     * 获取部门--总数
     * @return
     */
    static String orgDepartmentsCount(){
        String result = HttpUtils.getString(token.hostUrl + "/rest/orgDepartments/count/670869647114347?token="+token.getToken());

        System.out.println(result);
        return "";

    }
    /**
     * 按部门编码取部门信息
     */
    public static void getDeptByCode(Integer code) {
        String result = HttpUtils.getString(token.hostUrl + "/rest/orgDepartment/code/"+code+"?token="+token.getToken());
        System.out.println(result);
    }

    //按部门Id删除部门
    public static void getDeptByOrgCode(String orgCode){
        JSONObject header  = new JSONObject();
        header.set("token",token.getToken());

        JSONObject body = new JSONObject();
        body.set("token",token.getToken());
        JSONObject result = HttpUtils.sendDelete(token.hostUrl + "/rest/orgDepartment/"+orgCode,header,body);
        System.out.println(result);
    }

}
